package com.wrial.simple;
/*
 * @Author  Wrial
 * @Date Created in 15:24 2020/3/26
 * @Description 删除排序数组中的重复项（已拍好序）  使用双指针
 */

public class LeetCode_26 {


    // 使用双指针，p1用来记录需要的数组，p2负责向后遍历
    public int removeDuplicates(int[] nums) {


        int p1 = 0, p2 = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[p2] != nums[p1]) {
                nums[p1 + 1] = nums[p2];
                p1++;
            }
            p2++;
        }
        return p1 + 1;
    }

    public static void main(String[] args) {
        int[] ints = new int[]{0,0,1,1,1,2,2,3,3,4};

        System.out.println(new LeetCode_26().removeDuplicates(ints));
        for (int anInt : ints) {
            System.out.print(anInt + " " );
        }
    }

}
